import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';
import 'package:word_range_client/pojo/Result.dart';
import 'package:word_range_client/pojo/queery/WordQuery.dart';
import 'package:word_range_client/pojo/vo/WordBookVo.dart';
import 'package:word_range_client/repositories/WordBookRepository.dart';

import '../pojo/dto/PageDto.dart';

class WordBookViewModel{

  Future<Result> getAllBookBaseInfo() async {
    Result result = await WordBookRepository.getAllBookBaseInfo();
    return result;
  }

  Future<Result> getBookInfo(String id) async {
    Result result = await WordBookRepository.getBookInfo(id);
    return result;
  }

  Future<PageDto> getWordListOnBookId(WordQuery wordQuery) async {
    return await WordBookRepository.getWordListOnBookId(wordQuery);
  }

  Future<Result> getRandomWordByDay() async {
    Result result = await WordBookRepository.getRandomWordByDay();
    return result;
  }

  Future<Result> saveOrUpdateReview(String word) async {
    Result result = await WordBookRepository.saveOrUpdateReview(word);
    return result;
  }

  Future<List<Map<String, dynamic>>> getRandomNewWordList() async {
    final prefs = await SharedPreferences.getInstance();
    DateTime nowUtc = DateTime.now().toUtc();
    int lastReviewTime = prefs.getInt("last_review_time") ?? 0;
    DateTime lastReviewDate = DateTime.fromMillisecondsSinceEpoch(lastReviewTime).toUtc();

    // 如果今天已经获取过数据，或者prefs的数据为空，就读取prefs存储的数据返回
    if (lastReviewDate.year == nowUtc.year &&
        lastReviewDate.month == nowUtc.month &&
        lastReviewDate.day == nowUtc.day) {
      String? cachedData = prefs.getString("cached_word_list");

      if (cachedData != null) {
        return List<Map<String, dynamic>>.from(jsonDecode(cachedData));
      }
    }

    WordBookVo bookVo = WordBookVo.fromJson(jsonDecode(prefs.getString("selectBook")!));
    print("------------$bookVo");
    int count = prefs.getInt("newWordCount") ?? 10;

    // 否则就请求并保存到prefs并返回
    Result result = await WordBookRepository.getRandomNewWordList(bookVo.id!, count);
    if (result.data is List) {
      List<Map<String, dynamic>> wordList = List<Map<String, dynamic>>.from(result.data);
      prefs.setInt("last_review_time", nowUtc.millisecondsSinceEpoch);
      prefs.setString("cached_word_list", jsonEncode(wordList));
      return wordList;
    } else {
      throw Exception("返回的数据格式不正确");
    }
  }

  Future<List<Map<String, dynamic>>> getRandomReviewWordList() async {
    final prefs = await SharedPreferences.getInstance();
    DateTime nowUtc = DateTime.now().toUtc();
    int lastReviewTime = prefs.getInt("last_review_time_review") ?? 0;
    DateTime lastReviewDate = DateTime.fromMillisecondsSinceEpoch(lastReviewTime).toUtc();

    // 如果今天已经获取过数据，或者prefs的数据为空，就读取prefs存储的数据返回
    if (lastReviewDate.year == nowUtc.year &&
        lastReviewDate.month == nowUtc.month &&
        lastReviewDate.day == nowUtc.day) {
      String? cachedData = prefs.getString("cached_review_word_list");
      if (cachedData != null) {
        return List<Map<String, dynamic>>.from(jsonDecode(cachedData));
      }
    }

    WordBookVo bookVo = WordBookVo.fromJson(jsonDecode(prefs.getString("selectBook")!));
    int count = prefs.getInt("newWordCount") ?? 10;
    // 否则就请求并保存到prefs并返回
    Result result = await WordBookRepository.getRandomReviewWordList(bookVo.id!, count);
    if (result.data is List) {
      List<Map<String, dynamic>> wordList = List<Map<String, dynamic>>.from(result.data);
      prefs.setInt("last_review_time_review", nowUtc.millisecondsSinceEpoch);
      prefs.setString("cached_review_word_list", jsonEncode(wordList));
      return wordList;
    } else {
      throw Exception("返回的数据格式不正确");
    }
  }

  Future<List<Map<String, dynamic>>> getRandomReviewWordListByEvent(int count) async {
    Result result = await WordBookRepository.getRandomReviewWordListByEvent(count);
    return List<Map<String, dynamic>>.from(result.data);
  }

  Future<Result> getReviewedWordCount() async {
    return await WordBookRepository.getReviewedWordCount();
  }

  Future<Result> getMasteredWordCount() async {
    return await WordBookRepository.getMasteredWordCount();
  }

}